﻿using Bank.Operations;
using Bank.Reports;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bank.Account
{
    public class AccountDecorator: IAccount
    {
        public AccountDecorator(IAccount acc)
        {
            account = acc;
            Debt2 = new Money();
            Limit2 = new Money();
            
        }

        private decimal limit;

        public decimal Limit
        {
            get { return limit; }
            set { limit = value; }
        }

        private Money limit2;

        public Money Limit2
        {
            get { return limit2; }
            set { limit2 = value; }
        }

        private decimal debt;

        public decimal Debt
        {
            get { return debt; }
            set { debt = value; }
        }

        private Money debt2;

        public Money Debt2
        {
            get { return debt2; }
            set { debt2 = value; }
        }

        private IAccount account;

        public IAccount Account
        {
            get { return account; }
            set { account = value; }
        }
        

        public void CalculateMyInterest()
        {
            account.CalculateMyInterest();
        }

        public void Accept(IReport rep)
        {
            account.Accept(rep);
        }

        public void Accept2(IReport rep)
        {
            account.Accept2(rep);
        }

        public decimal Ballance
        {
            get
            {
                return account.Ballance - debt;
            }
            set
            {
                if (value > (account.Ballance - debt))
                {
                    if (debt > 0)
                    {
                        if (value > 0)
                        {
                            decimal newValue = value;
                            newValue = newValue - debt;
                            debt = 0;
                            account.Ballance = newValue;
                        }
                        else
                        {
                            debt = value;
                        }
                    }
                    else
                    {
                        account.Ballance = value;
                    }
                }
                else if (value < (account.Ballance - debt))
                {
                    if (value > 0)
                    {
                        account.Ballance = value;
                    }
                    else// if (value < 0)
                    {
                        if ((Math.Abs(value)) > limit)
                        {
                            throw new Exception("Reach max limit od Debet");
                        }
                        else
                        {
                            account.Ballance = 0;
                            debt = Math.Abs(value);
                        }
                    }
                }
            }
        }

        public Money Ballance2
        {
            get
            {
                return account.Ballance2 - debt2;
            }
            set
            {
                if (value > (account.Ballance2 - debt2))
                {
                    if (debt2 > 0)
                    {
                        if (value > 0)
                        {
                            Money newValue = value;
                            newValue = newValue - debt2;
                            debt2 = new Money();
                            account.Ballance2 = newValue;
                        }
                        else
                        {
                            debt2 = value;
                        }
                    }
                    else
                    {
                        account.Ballance2 = value;
                    }
                }
                else if (value < (account.Ballance2 - debt2))
                {
                    if (value > 0)
                    {
                        account.Ballance2 = value;
                    }
                    else// if (value < 0)
                    {
                        if ((Money.Abs(value)) > limit2)
                        {
                            throw new Exception("Reach max limit od Debet");
                        }
                        else
                        {
                            account.Ballance2 = new Money();
                            debt2 = Money.Abs(value);
                        }
                    }
                }
            }
        }

        public int Id
        {
            get
            {
                return account.Id;
            }
            set
            {
                account.Id = value;
            }
        }

        public int ClientId
        {
            get
            {
                return account.ClientId;
            }
            set
            {
                account.ClientId = value;
            }
        }

        public string Number
        {
            get
            {
                return account.Number;
            }
            set
            {
                account.Number = value;
            }
        }

        public List<String> Operationlist
        {
            get
            {
                return account.Operationlist;
            }
            set
            {
                account.Operationlist = value;
            }
        }

        public IInterestCalculator InterestCalculator
        {
            set
            {
                account.InterestCalculator = value;
            }
        }
    }
}
